// 图片地址
export let img = ''

// 请求地址
export let url = 'https://shangmen.qdxyqixy.com/api'

// page
export let page = 'https://shangmen.qdxyqixy.com/h5/'
// tabbar
// let tabarList = ["/pages/home/home", "/pages/technician/technician", "/pages/order/order", "/pages/user/user"];
// post请求
export let post = (api, data) => {
	// uni.showLoading();
	return new Promise(resolve => {
		uni.request({
			url: url + api,
			data,
			method: 'POST',
			header: {
				'content-type': 'application/json',
				'token': uni.getStorageSync('userToken'),
				'token-type': 0 //0正常token验证1跳过验证
			},
			success: (res) => {
				// uni.hideLoading();
				if (res.data.code == -1) {
					// let pages = getCurrentPages();
					// let route = '/' + pages[pages.length - 1].route;
					// if (route !== '/pages/login/login') {
					// 	let curParams = pages[pages.length - 1].options;
					// 	拼接参数
					// 	let param = ''
					// 	for (let key in curParams) {
					// 		param += '&' + key + '=' + curParams[key]
					// 	}
					// 	let flag = tabarList.includes(route);
					// 	uni.setStorageSync('currentUrl', JSON.stringify({
					// 		url: route + '?' + param,
					// 		flag: flag
					// 	}))
					// }

					uni.redirectTo({
						url: '/pages/login/login'
					});
					return
				}
				if (res.data.code == -2) {
					uni.setStorageSync('currentUrl', JSON.stringify({
						url: '/pages/home/home',
						flag: true
					}));
					uni.redirectTo({
						url: '/pages/phone/phone'
					});
					return;
				}
				if (res.data.code == 0) {
					uni.showToast({
						title: res.data.msg,
						icon: 'error',
						duration: 2000
					});
					return
				}
				resolve(res.data)
			},
			fail: (res) => {
				uni.showToast({
					title: '网络错误',
					icon: 'error',
					duration: 2000
				});
				// uni.hideLoading();
			}
		});
	})
}